/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.constant.CstScheme;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Follow;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.activity.CreateActivity;
import com.simon.mengkou.ui.dialog.DeleteDialog;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/4/1.
 * desc    : 发布图片适配器
 */
public class CreateImageAdapter extends AbsAdapter<String> {
    //最多图片数
    private static final int MAX_IAMGES = 8;
    private CreateActivity mActivity;

    public CreateImageAdapter(CreateActivity context) {
        super(context);
        mActivity  = context;
    }

    @Override
    public int getCount() {
        int count = UtilList.getCount(mList);
        if(count < MAX_IAMGES) {
            return count + 1;
        } else {
            return 8;
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_create_image_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final String data = getItem(position);

        int count = UtilList.getCount(mList);
        if(count < MAX_IAMGES) {//可继续添加图片
            if(position != getCount()-1) {
                //图片
                UtilLog.d("------------>>>"+data);
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage, CstScheme.FILE + data);
                holder.mSdvImage.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        final DeleteDialog dialog = new DeleteDialog(mActivity, null);
                        dialog.setOnClickListener(
                                new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if(v.getId() == R.id.delete_id_delete) {
                                            mList.remove(position);
                                            notifyDataSetChanged();
                                            dialog.dismiss();
                                        }
                                    }
                                });

                        dialog.showAtLocation(holder.mSdvImage, Gravity.CENTER, 0, 0);
                    }
                });
            } else {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage,
                        CstScheme.RES + mActivity.getPackageName() + File.separator + R.drawable.create_ic_add_image);
                holder.mSdvImage.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        mActivity.selectPicture();
                    }
                });
            }

        } else {//不能继续添加图片
            //图片
            OuerApplication.mImageLoader.loadImage(holder.mSdvImage, CstScheme.FILE +data);
            holder.mSdvImage.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    final DeleteDialog dialog = new DeleteDialog(mActivity, null);
                    dialog.setOnClickListener(
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if(v.getId() == R.id.delete_id_delete) {
                                        mList.remove(position);
                                        notifyDataSetChanged();
                                        dialog.dismiss();
                                    }
                                }
                            });

                    dialog.showAtLocation(holder.mSdvImage, Gravity.CENTER, 0, 0);
                }
            });
        }

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //图片
        @Bind(R.id.image_id_image)
        SimpleDraweeView mSdvImage;
    }
}
